﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ai_assignment_2
{
    class State
    {
        public static int N{get;set;}
        public List<Queen> Queens = new List<Queen>();

        public State(int n)
        {
            N = n;
        }
        public State(string s)
        {
            for (int j = 0; j<s.Length; j++)
            {
                Queen q = new Queen();
                q.Row = Convert.ToInt32(""+s[j]);
                q.Column = j;
                Queens.Add(q);     
            }
        }
    
        public List<State> getStatesForDistance()
        {
            List<State> l = new List<State>();
            for (int i = 0; i < N; i++)
            {
                State s = new State(this.ToString() + i);
                if (s.isValid())
                    l.Add(s);
            }
            return l;
        }

        public List<State> getStates()
        {
            List<State> l = new List<State>();
            for (int i = 0; i < N; i++)
            {
                State s = new State(this.ToString() +i);
                if (s.isValid())
                    l.Add(s);       
            }
            return l;
        }


        public override string ToString()
        {
            StringBuilder s = new StringBuilder(N);
            foreach (Queen q in Queens)
            {
                s.Append(q.Row);
            }
            return s.ToString();
        }
        public int[] addQueen()
        {
            int[] queenArr = new int[N];
            for(int i=0; i<Queens.Count; i++)
            {
                queenArr[i] = Queens[i].Row;       
            }
            return queenArr;
        }
        public bool isValid()
        {
            Queen[] queens = Queens.ToArray();
            for (int i = 0; i < queens.Length - 1; i++)
            {
                for (int j = i + 1; j < queens.Length; j++)
                { 
                    if(queens[i].isAttacking(queens[j])) 
                        return false;             
                }   
            }
            return true;
        }
        public bool isEnd()
        {
            return (isValid() && Queens.Count == N);      
        }

        public void printState()
        {
            string s = this.ToString();
            for (int i = 0; i < N; i++)
            {
                int k = Convert.ToInt32(""+s[i]);
                Console.WriteLine(String.Format("Queen {0}.{1}", i + 1,k+1));
            }
            Console.WriteLine();
        }

    }
}
